package com.yinhai.asynctask;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.yinhai.constant.UrlConstant;
import com.yinhai.model.IMGroup;
import com.yinhai.service.IMGroupServiceImpl;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

public class LoginInitGetUserGroupsTask  extends AsyncTaskForHttp{
	String retStr = null;
	private Context context;
	private IMGroupServiceImpl imGroupService;
	public LoginInitGetUserGroupsTask(Context context) {
		super(context,false);
		this.context = context;
		imGroupService = new IMGroupServiceImpl(context);
	}
	
	public LoginInitGetUserGroupsTask(Context context, boolean autoForword) {
		super(context, autoForword);
	}

	public LoginInitGetUserGroupsTask(Context context, int timeoutRequestCount,
			boolean autoForword) {
		super(context, timeoutRequestCount, autoForword);
	}
	@Override
	protected void parse(Map<String, Object> result) {
		this.retStr = (String)result.get(AsyncTaskContext.RESPONSE_CONTENT);
		JSONObject jo;
		try {
			jo = new JSONObject(retStr);
			JSONObject jo1 = null;
			if("0".equals(jo.getString("status"))) {
				JSONArray ja = jo.getJSONArray("dataList");
				IMGroup group = null;
				List<IMGroup> list = new ArrayList<IMGroup>();
				for(int i = 0; i < ja.length(); i++) {
					jo1 = ja.getJSONObject(i);
					//先删除历史数据
					imGroupService.delete(jo1.getString("group_id"));
					Map<String,Object> map=new HashMap<String, Object>();
					group = new IMGroup();
					group.setGroupId(jo1.getString("group_id"));
					group.setGroupName(jo1.getString("group_name"));
					group.setUserPhone(jo1.getString("user_phone"));
					list.add(group);
//					imGroupService.insert(group);
//					GetUserGroupListTask task = new GetUserGroupListTask(context,jo1.getString("group_id"));
//					List<NameValuePair> postParam = new ArrayList<NameValuePair>();
//					 NameValueParam groupIdp = new NameValueParam();
//					 groupIdp.setName("groupId");
//					 groupIdp.setValue(jo1.getString("group_id"));
//					 postParam.add(groupIdp);
//					 AsyncTaskParam getUserGroupList = new AsyncTaskParam(UrlConstant.LOCAL_URI
//								+ UrlConstant.GET_USER_GROUP_LIST);
//					 getUserGroupList.setPostParam(postParam);
//					 task.execute(getUserGroupList);
					
				}
				 SQLiteDatabase db = this.context.openOrCreateDatabase("im.db", Context.MODE_PRIVATE, null);  
			        db.beginTransaction();  
			        for(int j = 0; j < list.size(); j++) {
			        	 db.execSQL("INSERT INTO im_group(group_id,group_name,user_phone)" +
				        	 		" values(?, ?, ?)" ,  
				                     new Object[]{list.get(j).getGroupId(),list.get(j).getGroupName(),list.get(j).getUserPhone()});
					}
			        db.setTransactionSuccessful();  
			        db.endTransaction();  
			        db.close();
			} 
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
